package com.ilxqx.framework.security.strategy;

import com.ilxqx.framework.security.po.PermissionEntry;
import org.jetbrains.annotations.NotNull;

/**
 * 线程变量权限项保存策略
 * @author venus
 */
public class ThreadLocalMatchedPermissionEntryHolderStrategy implements MatchedPermissionEntryHolderStrategy {

    private static final ThreadLocal<PermissionEntry> HOLDER = new ThreadLocal<>();

    /**
     * 清除权限项
     */
    @Override
    public void clearPermissionEntry() {
        HOLDER.remove();
    }

    /**
     * 获取权限项
     *
     * @return 权限项
     */
    @Override
    public PermissionEntry getPermissionEntry() {
        PermissionEntry permissionEntry = HOLDER.get();
        if (permissionEntry == null) {
            permissionEntry = new PermissionEntry();
        }
        return permissionEntry;
    }

    /**
     * 设置权限项
     *
     * @param permissionEntry 权限项
     */
    @Override
    public void setPermissionEntry(@NotNull PermissionEntry permissionEntry) {
        HOLDER.set(permissionEntry);
    }

}
